#include "GBButton.h"

GBButton::GBButton() : m_size(vec2(1.0f, 1.0f))
{

}

GBButton::~GBButton()
{

}

void GBButton::Update(const float fTimeStep)
{
	m_label.Update(fTimeStep);

	// Auto shape the collision volume to the text width/height of the label
	if(m_label.GetFont() && m_bIsAutoSized)
	{
		const GBFont* pFont = m_label.GetFont();

		const std::vector<GBLabel::LabelString>& strings = m_label.GetString();

		unsigned numStrings = (unsigned)strings.size();

		// Special case for Y's
		if(numStrings <= 1)
			m_size.Y() = pFont->GetTextHeight(strings[0].string.c_str());
		else m_size.Y() = Max(numStrings * m_label.GetLineSpacing(), 1.0f);

		// Setup X's
		float longest = 0.0f;
		for (unsigned i = 0; i < numStrings; i++)
		{
			float current = pFont->GetTextWidth(strings[i].string.c_str());

			if(current > longest)
				longest = current;
		}

		m_size.X() = Max(longest, 1.0f);
	}
}

void GBButton::Render()
{
	// Update label's members to button's members
	m_label.SetAlignment(this->m_AlignType);
	m_label.GetLocalMatrix().MakeTranslation(this->m_local.wAxis());
	m_label.SetColor(this->m_color);
	m_label.SetSpeed(this->m_fSpeed);
	m_label.SetVelocity(this->m_velocity);
	m_local.MakeTranslation(m_label.GetLocalMatrix().wAxis());

	m_label.Render();
}

void GBButton::Copy(const GBWidget* pRef)
{
	if(!pRef) return;

	GBWidget::Copy(pRef);

	GBButton* pButton = (GBButton*)pRef;

	// Copy over specific Button attribs / members
	this->m_size = pButton->m_size;
	this->m_label.Copy(&pButton->m_label);
}